package com.fatcat.easybuy.service.impl;

import com.fatcat.easybuy.exception.BusinessException;
import com.fatcat.easybuy.mapper.EasyBuyOrderMapper;
import com.fatcat.easybuy.mapper.EasyBuyProductMapper;
import com.fatcat.easybuy.pojo.EasyBuyOrder;
import com.fatcat.easybuy.pojo.EasyBuyOrderDetail;
import com.fatcat.easybuy.pojo.EasyBuyProduct;
import com.fatcat.easybuy.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 郝秦毅
 * @since 2025/9/21 19:27
 */
@Service
public class AiPayServiceImpl implements AliPayService {

    @Autowired
    private EasyBuyOrderMapper easyBuyOrderMapper;

    @Autowired
    private EasyBuyOrderDetailService easyBuyOrderDetailService;

    @Autowired
    private EasyBuyProductService easyBuyProductService;
    @Autowired
    private ProductEsRepository productEsRepository;


    @Transactional
    @Override
    public boolean handlePayNotify(String outTradeNo, String tradeNo) {
        EasyBuyOrder order = easyBuyOrderMapper.selectOrderBySerialNumber(outTradeNo);
        // 更新订单状态
        order.setStatus(2);
        order.setAlipayTradeNo(tradeNo);
        try {
            if (easyBuyOrderMapper.updateOrder(order) <= 0) {
                throw new BusinessException("更新订单状态失败");
            }
            // 回滚预出库库存
            if (!easyBuyProductService.updateProductTempStockRollbackByOutOrderNo(outTradeNo)) {
                throw new BusinessException("回滚预出库库存失败");
            }
            //更新es库存
            List<EasyBuyOrderDetail> orderDetails = easyBuyOrderDetailService.getOrderDetailByOrderId(order.getId());
            List<Long> productIds = new ArrayList<>();
            for (EasyBuyOrderDetail orderDetail : orderDetails) {
                productIds.add(orderDetail.getProductId());
            }
            List<EasyBuyProduct> products = easyBuyProductService.getProductsByIds(productIds);
            productEsRepository.saveAll(products);

            return true;
        } catch (Exception e) {
            throw new BusinessException("更新订单状态或回滚预出库库存失败", e);
        }
    }

    @Override
    public boolean handleRefundNotify(String outTradeNo) {
        EasyBuyOrder order = easyBuyOrderMapper.selectOrderBySerialNumber(outTradeNo);
        //更新订单状态
        order.setStatus(4);
        try {
            if (easyBuyOrderMapper.updateOrder(order) <= 0) {
                throw new BusinessException("更新订单状态失败");
            }
            //更新库存
            if (!easyBuyProductService.updateProductStockRollbackByOutOrderNo(outTradeNo)) {
                throw new BusinessException("更新订单状态失败");
            }
            //更新es库存
            List<EasyBuyOrderDetail> orderDetails = easyBuyOrderDetailService.getOrderDetailByOrderId(order.getId());
            List<Long> productIds = new ArrayList<>();
            for (EasyBuyOrderDetail orderDetail : orderDetails) {
                productIds.add(orderDetail.getProductId());
            }
            List<EasyBuyProduct> products = easyBuyProductService.getProductsByIds(productIds);
            productEsRepository.saveAll(products);
            //清空购物车

            return true;
        } catch (Exception e) {
            throw new BusinessException("更新库存失败", e);
        }
    }
}
